package com.flyllp.common.mail;

import java.util.Date;
import java.util.LinkedList;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

public class MailSender
{
	private final static Logger logger = Logger.getLogger("MailSender");

	private final static String CONTENT_TYPE = "text/html; charset=utf-8";

	private static Session mailSession = null;

	private static LinkedList<Message> messageList = new LinkedList<Message>();

	@Autowired
	private MailConfig mailConfig;

	public Message createMessage()
	{
		try
		{
			Message mailMessage = new MimeMessage(getMailSession());
			Address from = new InternetAddress(mailConfig.getFromAddress());
			mailMessage.setFrom(from);
			mailMessage.setSentDate(new Date());
			return mailMessage;
		}
		catch (MessagingException ex)
		{
			logger.error("", ex);
			ex.printStackTrace();
		}
		return null;

	}

	public void mailToQueue(MailSenderInfo info)
	{
		try
		{
			Message mailMessage = createMessage();
			Address to = new InternetAddress(info.getToAddress());
			mailMessage.setRecipient(Message.RecipientType.TO, to);
			mailMessage.setSubject(info.getSubject());
			Multipart mainPart = new MimeMultipart();
			BodyPart html = new MimeBodyPart();
			html.setContent(info.getContent(), CONTENT_TYPE);
			mainPart.addBodyPart(html);
			mailMessage.setContent(mainPart);
			messageList.addFirst(mailMessage);
		}
		catch (MessagingException ex)
		{
			logger.error("", ex);
			ex.printStackTrace();
		}
	}

	public void sendMail()
	{
		try
		{
			Transport trans = getMailSession().getTransport(new InternetAddress());
			trans.connect();
			while(!messageList.isEmpty())
			{
				Message message = messageList.getLast();
				trans.sendMessage(message, message.getAllRecipients());
				messageList.removeLast();
			}
			
			trans.close();
		}
		catch (MessagingException ex)
		{
			logger.error("", ex);
			ex.printStackTrace();
		}
	}

	private Session getMailSession()
	{
		if (mailSession == null)
		{
			Authenticator authenticator = null;
			if ("true".equals(mailConfig.getValidate()))
			{
				authenticator = new Authenticator()
				{
					protected PasswordAuthentication getPasswordAuthentication()
					{
						return new PasswordAuthentication(mailConfig.getUserName(), mailConfig.getPassword());
					}
				};
			}
			Properties pro = mailConfig.getProperties();
			mailSession = Session.getDefaultInstance(pro, authenticator);
		}
		return mailSession;
	}

}
